#!/bin/bash

source /AIR_FLASH/script/air.conf
source /AIR_FLASH/script/shcommon
source /AIR_FLASH/system/air_sys.conf


if [ ${dataCollect_usb} -le 0 ]  ; then
    exit
fi
 
USB_STICK_DEV_FILE="/dev/sdb1"
USB_STICK_DISK="/usbdisk"
USB_STICK_FOR_6A="/6A"
USB_DISK_LICENSE="FALSE"
USB_DISK_INSERT="FALSE"
USB_RSYNC_START_USB="FALSE"
USB_RSYNC_START_6A="TRUE"

UPLOAD_DIR="/upload"
UPLOAD_DISK_MOUNT_FLAG="FALSE"

USB_SATA_DEV_FILE="/dev/sda2"
USB_6A_DEV_FILE="/dev/sda1"

readyPath="/upload/data"
RSYNC_6A_LISTS="/upload/video/.6ARsyncFileLists"
RSYNC_6A_OLDLIST="/upload/video/.oldLists"
rsync_lastFiles="/upload/video/.lastFiles"
rsync_notListFlg="/tmp/.notlist"
A6FILE_ALL="/upload/video/.6AfilesAll"
#//交换
isUPan="/tmp/.isUpan"
rsync_reserveLastTimeAdd="/tmp/.reg6AsubTime"
#first exec
USB_SYNC_TICKS=15
USB_SYNC_TIME=15
SDA_SYNC_TICKS=60
SDA_SYNC_TIME=60
fatal_error_6A_times=0
fatal_error_upan_times=0
fatal_error_times=0
if [ ${dataCollect_usb} -le 0 ] ; then
    exit
fi
function usb_init()
{
        if [ ! -d "${upan_back_path}" ] ; then
            mkdir -p "${upan_back_path}"
        fi
        rm ${transfer_path}/6A* -fr
        rm ${upan_back_path}/* -fr
        if [ -f "${transfer_path}/.6Aerror" ] ; then     #出错时同步/6A中所有数据
            rsync -av /6A/. ${upload_path}/. --exclude-from=/AIR_FLASH/script/rsync.conf > /dev/null 2>&1
            rm "${transfer_path}/.6Aerror" -fr 
            #tmp huafen block  
            echo "`date +%Y-%m-%d` `date +%H:%M:%S` reading /6A files,when power on and 6Aerror" >> ${A6List_LOG}           
            cd /6A
            find ./ -name '*.mp4' -or -name '*.idx' | xargs -i  ls '{}' > /tmp/.6AerrRsync
            cd /
            while read line
            do
                ls -l --time-style '+%Y-%m-%d %H:%M:%S' /6A/${line} >> ${A6List_LOG}        
            done < /tmp/.6AerrRsync            
            rm -f /tmp/.6AerrRsync  
        else #正常，同步未同步的数据
            if [ -f "${rsync_notList}" ] ; then
                rsync -av --files-from="${rsync_notList}" /6A/. ${upload_path}/. > /dev/null 2>&1        
                echo "`date +%Y-%m-%d` `date +%H:%M:%S` /upload/data add file,when power on" >> ${A6List_LOG}
                while read line
                do
                    ls -l --time-style '+%Y-%m-%d %H:%M:%S' ${upload_path}/${line} >> ${A6List_LOG}
                done < ${rsync_notList}
                rm -f ${rsync_notList}
            fi
        fi

        if [ "${HWVersion}" == "V0" ] ; then #qiantu
            clientid=`cat /proc/cmdline |awk '{print $5}'`
        elif [ "${HWVersion}" == "V1" ] ; then #diange    
            clientid=`cat /proc/cmdline |awk '{print $6}'`
        fi
        if [ -f "${log_path}/kern.log" ] ; then
            t=`cat "${log_path}/kerntime"`
            rm "${log_path}/kerntime" -f
            mv "${log_path}/kern.log" "${upload_path}/kern_${clientid}_${t}.log"    
        fi
        if [ -f "${transfer_path}/.needmkfs" ] ; then
            if [ -b "${USB_SATA_DEV_FILE}" ] ; then
                umount ${USB_6A_DEV_FILE}
                mkfs.fat ${USB_6A_DEV_FILE}
                if [ "${HWVersion}" != "V0" ] ; then
                    mount ${USB_6A_DEV_FILE} /6A
                else
                    mount -o codepage=936,iocharset=utf8 ${USB_6A_DEV_FILE} /6A
                fi
                echo "need mkfs ${USB_6A_DEV_FILE}" >> $AIR_LOG
                rm -f "${transfer_path}/.needmkfs"
            fi
        fi
    
        rm /6A/* -fr
        rm ${transfer_path}/.* -fr
        rm ${transfer_path}/* -fr 
        cp /AIR_FLASH/license.txt /6A/license.txt

        cd $prefix/usb/
        lsmod | grep libcomposite > /dev/null
        if [ $? -ne 0 ] ; then
            insmod /AIR_FLASH/usb/libcomposite.ko > /dev/null 2>&1
        fi
        lsmod | grep usb_f_mass_storage > /dev/null
        if [ $? -ne 0 ] ; then
            insmod /AIR_FLASH/usb/usb_f_mass_storage.ko > /dev/null 2>&1
        fi
        lsmod | grep g_mass_storage > /dev/null
        if [ $? -ne 0 ] ; then
            insmod /AIR_FLASH/usb/g_mass_storage.ko file=${USB_6A_DEV_FILE} removable=1 > /dev/null 2>&1
        fi
}
if [ ! -f "/tmp/usbInitFlag" ] ; then
    usb_init
    touch "/tmp/usbInitFlag"
fi
function self_mount()
{
    if [ "${HWVersion}" != "V0"  ] ; then
        mount $1 $2
    else
        mount -o codepage=936,iocharset=utf8 $1 $2    
    fi
}

function clean_sda1()
{
    Error "${USB_6A_DEV_FILE} full,rmmod driver start"      
    rmmod g_mass_storage.ko > /dev/null 2>&1
    rmmod usb_f_mass_storage.ko > /dev/null 2>&1 
    rmmod libcomposite.ko > /dev/null 2>&1 
    
    rm ${USB_STICK_FOR_6A}/* -fr > /dev/null 2>&1
    
    cp /AIR_FLASH/license.txt  $USB_STICK_FOR_6A/license.txt > /dev/null 2>&1

    insmod /AIR_FLASH/usb/libcomposite.ko > /dev/null 2>&1
    insmod /AIR_FLASH/usb/usb_f_mass_storage.ko > /dev/null 2>&1
    insmod /AIR_FLASH/usb/g_mass_storage.ko file=${USB_6A_DEV_FILE} removable=1 > /dev/null 2>&1
    Error "${USB_6A_DEV_FILE} full,rmmod driver end"   
}

#SATA错误检查1
function fatal_error_sda()
{
    if [ ! "$fatal_error_times" ] ; then
        fatal_error_times=0
    fi
	
    fatal_error_times=$[$fatal_error_times+1]

    if [ $fatal_error_times -gt 5 ] ; then
        Error "/dev/sda fatal error $fatal_error_times times"		
    fi
}
#SATA只读或满判断
function check_SATA_sda_RO()
{
    findmnt $USB_SATA_DEV_FILE | grep ro,  > /dev/null 2>&1   #sda2 readonly
    if [ $? -eq 0 ] ; then        
        Error "findmnt $USB_SATA_DEV_FILE readonly,force umount $USB_SATA_DEV_FILE /upload"
        fuser -k /upload > /dev/null 2>&1
        fuser -k $USB_SATA_DEV_FILE  > /dev/null 2>&1
        umount $USB_SATA_DEV_FILE  > /dev/null 2>&1
        fsck.ext4 -a ${USB_SATA_DEV_FILE}
        fatal_error_sda
    fi
    findmnt $USB_6A_DEV_FILE | grep ro,  > /dev/null 2>&1    #sda1 readonly
    if [ $? -eq 0 ] ; then      
        Error "findmnt $USB_6A_DEV_FILE readonly,force umount $USB_6A_DEV_FILE /6A"
        fuser -k /6A > /dev/null 2>&1
        fuser -k $USB_6A_DEV_FILE  > /dev/null 2>&1
        umount $USB_6A_DEV_FILE  > /dev/null 2>&1            
        fatal_error_sda       
    fi
    pNum=$(df | grep $USB_6A_DEV_FILE | awk '{print int($5)}')
    if  [[ $pNum ]] && [[ $pNum -ge 99 ]] ; then        
        if [ -f "/tmp/.sda1full.tmp" ] ; then
            Error "$USB_6A_DEV_FILE not enough space to write remove data in /6A"
            clean_sda1
            rm -f "/tmp/.sda1full.tmp"
        fi
    fi
}
#SATA盘出差判断
function check_SATA_sda()
{
    tmpcunt=0
    findmnt $USB_SATA_DEV_FILE | grep /upload  > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        ls /upload > /dev/null 2>&1
        if [ $? -ne 0 ] ; then
            errsum=`tail -n 20 /var/log/kern.log | grep blk_update_request | grep 'I/O error' | grep ${USB_SATA_DEV_FILE} | grep sector | wc -l`
            if [[ $errsum ]] && [[ $errsum -gt 0 ]] ; then
                Error "ls $USB_SATA_DEV_FILE /upload blk_update_request: I/O error, dev sda, sector"
                fatal_error_sda		
            else
                tmpcunt=1
            fi
        else
            tmpcunt=1
        fi
    fi
    findmnt $USB_6A_DEV_FILE | grep /6A  > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        ls /6A > /dev/null 2>&1
        if [ $? -ne 0 ] ; then
            errsum=`tail -n 20 /var/log/kern.log | grep blk_update_request | grep 'I/O error' | grep ${USB_6A_DEV_FILE} | grep sector | wc -l`
            if [[ $errsum ]] && [[ $errsum -gt 0 ]] ; then
                Error "ls $USB_6A_DEV_FILE /6A blk_update_request: I/O error, dev sda, sector"
                fatal_error_sda		
            else
                tmpcunt=2
            fi
        else
            tmpcunt=2
        fi 
    fi
    if [ $tmpcunt -eq 2 ] ; then
        fatal_error_times=0
    fi
	
}
function rsync_filelist()
{
    tmpFileList=/tmp/.findmp4
    if [ ! -d $1 ] ; then
        return
    fi
    cd ${transfer_path}
    ts=`du -s | awk '{print $1}'`
    if [[ ! $ts ]] || [[ $ts -lt 4 ]] ; then
        return
    fi
    find ./ -name '*.mp4' -or -name '*.idx' | xargs -i  ls -l --time-style '+%s' '{}' | cut -d" " -f6- | sort -r > $tmpFileList

    if [ -f "${transfer_path}/.profile" ] ; then
        source  ${transfer_path}/.profile
    fi
    if [ ! $baseTime ] ; then
       baseTime=1
    fi
    bTime=${baseTime}
   # i=0
    while read line
    do
        t=`echo ${line} | awk '{print int($1)}'`
        if [ $t -gt $bTime ] ; then           
            subFile=${line:${#t}} 
            subFile=${subFile:1}           
            echo $subFile
        fi
        if [ $baseTime -lt $t ] ; then
              baseTime=$t
        fi
        #i=$[$i+1]
        #if [ $i -eq 1 ] ; then
        #   baseTime=$t
        #fi  
    done < $tmpFileList
    if [ $baseTime -gt 1 ] ; then 
        echo "baseTime=$baseTime" >  ${transfer_path}/.profile
    fi

    return
}

function rsync_TCMS()
{
    cd ${transfer_path}
    tsize=0
    tmpFileList=/tmp/.findtcms
    find ./ -name '*.dat' -or -name '*.eve' > $tmpFileList
    while read line
    do
        line=${line:2}  
        path=${line%/*} 
        OLD_IFS="$IFS"
        IFS="/"
        arr=($path)
        IFS="$old_IFS"
        for i in "${!arr[@]}"
        do
            j=0
            subpath=""
            while [ $j -le $i ]
            do
                subpath="${subpath}${arr[j]}/"
                j=$[$j+1]
            done 
            if [ -n ${subpath} ] ; then
                if [ ! -d "${transfer_path}/${subpath}" ] ; then
                    continue;
                fi
                if [ ! -d "${upload_path}/${subpath}" ] ; then
                    mkdir -p "${upload_path}/${subpath}" > /dev/null 2>&1
                fi
            fi
        done
        ln -if "${transfer_path}/${line}" "${upload_path}/${line}"
        rm -f "${transfer_path}/${line}"
        st=`ls -l "${upload_path}/${line}" | awk '{print $5}'`
        if [[ ${st} ]] && [[ ${st} -gt 0 ]] ; then
            tsize=$[$tsize+$st]         
        fi  
    done < $tmpFileList
    rm -f $tmpFileList
    return $tsize
}
#同步到/upload/data 硬链接
function sync_sda2()
{
    if [ ! -f $1 ] ; then
        return
    fi
    num=`cat $1 | wc -l`
    if [[ ! $num ]] || [[ $num -eq 0 ]] ; then
        rm -f $1
        return
    fi
   
    if [ -f ${isUPan} ] ; then    
        echo "`date +%Y-%m-%d` `date +%H:%M:%S` /upload/Upan add link file" >> ${UpanList_LOG}  
    fi 

    while read line
    do
        cd ${upload_path}
        line=${line:2}  
        path=${line%/*} 
        OLD_IFS="$IFS"
        IFS="/"
        arr=($path)
        IFS="$old_IFS"
        for i in "${!arr[@]}"
        do
            j=0
            subpath=""
            while [ $j -le $i ]
            do
                subpath="${subpath}${arr[j]}/"
                j=$[$j+1]
            done 
            if [ -n ${subpath} ] ; then
                if [ ! -d "${transfer_path}/${subpath}" ] ; then
                    continue;
                fi
                if [ ! -d "${upload_path}/${subpath}" ] ; then
                    mkdir -p "${upload_path}/${subpath}" > /dev/null 2>&1
                fi
                if [ ! -d "${upan_back_path}/${subpath}" ] ; then
                    mkdir -p "${upan_back_path}/${subpath}" > /dev/null 2>&1
                fi
            fi
        done 
        ln -if "${transfer_path}/${line}" "${upload_path}/${line}" > /dev/null 2>&1    
        if [ -f ${isUPan} ] ; then    
            ln -if "${transfer_path}/${line}" "${upan_back_path}/${line}" > /dev/null 2>&1
            ls -l --time-style '+%Y-%m-%d %H:%M:%S' "${upan_back_path}/${line}" >> ${UpanList_LOG} 
        fi
    done < $1  
    rm -f $1
}
#同步到U盘
function sync_usbdisk()
{ 
    rsync -av --remove-source-files --bwlimit=5000 ${upan_back_path}/. ${USB_STICK_DISK}/. > $RSYNC_tmp 2>&1  
    grep "rsync:" | grep "error" < $RSYNC_tmp
    if [ $? -eq 0 ]; then           
        Error "rsync: read errors mapping ${fatal_error_upan_times} times---usbdisk kern.log---"     
        cat /var/log/kern.log | grep "FAT-fs" | tail -n 1 >> $SH_LOG
        grep -v "${Netbond}\|${POE1}\|${POE2}" /var/log/kern.log > "${log_path}/kern.log"
        echo "`date +%Y%m%d`-`date +%H%M%S`" > "${log_path}/kerntime"           
        Error "-----usbdisk rsync.log--------"  
        cat $RSYNC_tmp >> $SH_LOG
        
        umount ${USB_STICK_DISK}      
        fsck.fat -a ${USB_STICK_DEV_FILE}
        tdev=`fdisk -l  | grep /dev/sd | grep -v "sda\|Disk" |tail -n 1 | awk '{print $1}'`
        if [ -n $tdev ]; then
            USB_STICK_DEV_FILE=$tdev	
        fi
        self_mount ${USB_STICK_DEV_FILE} ${USB_STICK_DISK}
        rm -f ${USB_STICK_DISK}/FSCK* > /dev/null 2>&1
        rm -f ${USB_STICK_DISK}/fsck* > /dev/null 2>&1
	fatal_error_upan_times=$[$fatal_error_upan_times+1]	
    fi	
    echo "" > $RSYNC_tmp
}

function rsync_6A_filelist()
{    
    if [ ! -f $1 ] ; then
        return
    fi
    
    lastT=`date '+%s'`   
    lastT=$[$lastT+28800] 
    
    lastT=$[$lastT-${rsync_reserveLastTime}]  
    if [ -f ${rsync_reserveLastTimeAdd} ] ; then
        source ${rsync_reserveLastTimeAdd}
        lastT=$[$lastT-${regSub6ATime}] 
    fi 
    rm -f ${RSYNC_6A_LISTS}
    rm -f ${rsync_notList}
    while read line
    do
        t=`echo ${line} | awk '{print int($1)}'`
        subFile=`echo ${line} | cut -d" " -f2-`
        if [ $t -lt $lastT ] ; then 
            echo $subFile >> ${RSYNC_6A_LISTS}           
        else
            echo $subFile >> ${rsync_notList}
        fi
    done < $1
}
function new_6A_fileLists()
{
    tmpFileList="/tmp/.find6Amp4"
    ts=`du -s ${USB_STICK_FOR_6A} | awk '{print $1}'`
    if [[ ! $ts ]] || [[ $ts -lt 4 ]] ; then
        return
    fi
    cd ${USB_STICK_FOR_6A}  
    find ./ -name '*.mp4' -or -name '*.idx' | xargs -i  ls -l --time-style '+%s' '{}' | cut -d" " -f6- | sort -r > ${tmpFileList}
    cd ${transfer_path} 
#${RSYNC_6A_LISTS} #< 15min
#${rsync_notList}  #> 15min
#${RSYNC_6A_OLDLIST} # rsyned filesss
    rsync_6A_filelist ${tmpFileList}   #rsynclist  nosync
    i=0
    rm -fr "${rsync_lastFiles}"
    rm -fr "${tmpFileList}"
    if [ ! -f "${RSYNC_6A_LISTS}" ] ; then
        return;	
    fi
    while read line
    do
        if [ ! -f "${RSYNC_6A_OLDLIST}" ] ; then
            cat "${RSYNC_6A_LISTS}" >> "${rsync_lastFiles}"
            break
        else
            tfl=`cat "${RSYNC_6A_OLDLIST}" | grep "${line}" | wc -l`
            if [ $tfl -eq 1 ] ; then             
                if [ $i -ge 14 ] ; then
                    break
                fi
                i=$[$i+1]
            else
                echo ${line} >> "${rsync_lastFiles}" 
            fi
        fi
    done < ${RSYNC_6A_LISTS} 
    #RSYNC_6A_CURLIST
    if [ -f  "${rsync_lastFiles}" ] ; then         
        echo "`date +%Y-%m-%d` `date +%H:%M:%S` /6A add file" >> ${A6List_LOG} 
        while read line
        do
             ls -l --time-style '+%Y-%m-%d %H:%M:%S' /6A/${line} >> ${A6List_LOG}
        done < ${rsync_lastFiles}         
    fi
}
function log_6A_fileLists()
{
    tmpFileList=/tmp/.find6Amp4                                                          
     
    ts=`du -s ${USB_STICK_FOR_6A} | awk '{print $1}'`
    if [[ ! $ts ]] || [[ $ts -lt 4 ]] ; then
        return
    fi
    cd ${USB_STICK_FOR_6A}                                                                                                             
    find ./ -name '*.mp4' -or -name '*.idx' | xargs -i  ls -l --time-style '+%s' '{}' | cut -d" " -f6- | sort -r > ${tmpFileList}
    cd ${transfer_path} 

    rsync_6A_filelist ${tmpFileList} 
    i=0                                                                                                                       
    while read line                                                                                                                
    do                                                                                                                             
        if [ ! -f "${A6FILE_ALL}" ] ; then                                                                       
            cat "${tmpFileList}" | cut -d" " -f2- >> "${transfer_path}/.filesList"
            break                                                                 
        else                                                                                                                       
           subFile=`echo ${line} | cut -d" " -f2-`                                                                              
            tfl=`cat "${A6FILE_ALL}" | grep "${subFile}" | wc -l`                                                 
            if [ $tfl -eq 1 ] ; then                                                                                            
                if [ $i -ge 14 ] ; then
                    break
                fi
                i=$[$i+1]                                                                                                         
            else                                                                                                                
                 echo ${subFile} >> "${transfer_path}/.filesList"                                            
            fi                                                                                                                
        fi                                                                                                                         
    done < ${tmpFileList}                                                                                                           
    if [ -f  "${transfer_path}/.filesList" ] ; then 
        cat "${A6FILE_ALL}" > "${transfer_path}/.filesListTmp"                                                
        cat "${transfer_path}/.filesList" >> "${transfer_path}/.filesListTmp"                                                  
        sort -k2n "${transfer_path}/.filesListTmp" | uniq > "${A6FILE_ALL}"                                   
        rm -f "${transfer_path}/.filesListTmp" 
        echo "`date +%Y-%m-%d` `date +%H:%M:%S` /6A add file" >> ${A6List_LOG} 
        while read line
        do
             ls -l --time-style '+%Y-%m-%d %H:%M:%S' /6A/${line} >> ${A6List_LOG}
        done < ${transfer_path}/.filesList 
        rm -f ${transfer_path}/.filesList                                                      
    fi
                                                                                                                                 
}
#sdb1 u盘
#sdb2 sata盘
function check_sda1()
{
    if [ -b ${USB_6A_DEV_FILE} ]; then		
        findmnt ${USB_6A_DEV_FILE} | grep ${USB_STICK_FOR_6A} > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            if [ ! -d ${USB_STICK_FOR_6A} ]; then			
                mkdir -p ${USB_STICK_FOR_6A}
            fi
            umount ${USB_6A_DEV_FILE} > /dev/null 2>&1
            self_mount ${USB_6A_DEV_FILE} ${USB_STICK_FOR_6A}	> /dev/null 2>&1		
            findmnt ${USB_6A_DEV_FILE} | grep ${USB_STICK_FOR_6A} > /dev/null
            if [ $? -ne 0 ]; then
                Error "mount "${USB_6A_DEV_FILE}" "${USB_STICK_FOR_6A}" FAILED"
                fatal_error_sda					
            #else
            #    Info "mount "${USB_6A_DEV_FILE}" "${USB_STICK_FOR_6A}" SUCCESSED"               
            fi
            cp /AIR_FLASH/license.txt  ${USB_STICK_FOR_6A}/license.txt > /dev/null 2>&1
        fi
        cat /6A/license.txt | grep Username > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            cp /AIR_FLASH/license.txt  ${USB_STICK_FOR_6A}/license.txt > /dev/null 2>&1
        fi
        cat /6A/license.txt | grep Password > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            cp /AIR_FLASH/license.txt  ${USB_STICK_FOR_6A}/license.txt > /dev/null 2>&1
        fi
        cat /6A/license.txt | grep Channel > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            cp /AIR_FLASH/license.txt  ${USB_STICK_FOR_6A}/license.txt > /dev/null 2>&1
        fi
        return 1
    else
        return 2
    fi
}
function check_sda2()
{
    if [ -b ${USB_SATA_DEV_FILE} ]; then
        #检查是否已经mount好了
        findmnt ${USB_SATA_DEV_FILE} | grep ${UPLOAD_DIR} > /dev/null
        if [ $? -ne 0 ]; then
	    if [ ! -d "${UPLOAD_DIR}" ]; then
                mkdir -p ${UPLOAD_DIR}               
    	    fi  
            umount ${USB_SATA_DEV_FILE} > /dev/null 2>&1
            mount ${USB_SATA_DEV_FILE} ${UPLOAD_DIR} > /dev/null 2>&1
            findmnt ${USB_SATA_DEV_FILE} | grep ${UPLOAD_DIR} > /dev/null
            if [ $? -ne 0 ]; then
	        Error "mount "${USB_SATA_DEV_FILE}" ${UPLOAD_DIR} FAILED"				
            #else	
	    #    Info "mount "$USB_SATA_DEV_FILE" ${UPLOAD_DIR} SUCCESSED"               
            fi
            rm -f ${UPLOAD_DIR}/FSCK* > /dev/null 2>&1  
            rm -f ${UPLOAD_DIR}/fsck* > /dev/null 2>&1                 
        fi
        return 1
    else
        return 2
    fi
}

#driver

while true
do
    sleep 1
	USB_SYNC_TICKS=$[$USB_SYNC_TICKS+1] 
	#判断USB盘是否插入
	if [ $USB_DISK_INSERT = "FALSE" ]; then
        #tdev=$(cat /proc/partitions  | grep sd[b,c,d,e,f,g,h,i,j,k,l,m,n]1 | awk '{print $4}')
        tdev=`fdisk -l  | grep /dev/sd | grep -v "sda\|Disk" |tail -n 1 | awk '{print $1}'`
        if [ -n $tdev ]; then
            USB_STICK_DEV_FILE=$tdev	
        fi
        if [[ -n ${USB_STICK_DEV_FILE} ]] && [[ -b $USB_STICK_DEV_FILE ]]; then
            USB_DISK_INSERT="TRUE"
            usbInfo=`cat /var/log/kern.log | grep "Direct-Access" | awk 'END {print}'`
            Info "***detect usb plug***"	           
            echo "" > ${isUPan}  
            echo "***detect usb plug***" >> ${UpanList_LOG}    
           # rm -fr ${upan_back_path}/*  	
        fi		
	fi
	#USB盘是否拔出
    if [ $USB_DISK_INSERT = "TRUE" ]; then
        #tdev=$(cat /proc/partitions  | grep sd[b,c,d,e,f,g,h,i,j,k,l,m,n]1 | awk '{print $4}')
        tdev=`fdisk -l  | grep /dev/sd | grep -v "sda\|Disk" |tail -n 1 | awk '{print $1}'`
        if [[ -z $tdev ]] || [[ ! -b $tdev ]]; then
            #if [ ! -b $USB_STICK_DEV_FILE ]; then
            USB_DISK_INSERT="FALSE"
            USB_DISK_LICENSE="FALSE"
            fuser -k $USB_STICK_DISK > /dev/null 2>&1
            umount $USB_STICK_DISK > /dev/null 2>&1
            umount $USB_STICK_DEV_FILE > /dev/null 2>&1
            findmnt $USB_STICK_DEV_FILE | grep $USB_STICK_DISK > /dev/null
            if [ $? -eq 0 ]; then
                    #Info "umount "$USB_STICK_DEV_FILE" SUCCESSED"
           # else
                    Error "umount "$USB_STICK_DEV_FILE" FAILED"
            fi              
            rm -rf $USB_STICK_DISK > /dev/null 2>&1          
            USB_RSYNC_START_USB="FALSE"
            Info "***detect usb unplug***"             
           # rm -f ${isUPan}
            echo "***detect usb unplug***" >> ${UpanList_LOG}
            		
        fi
    fi	
    #usbdisk检查 挂载
    if [ -b $USB_STICK_DEV_FILE ]; then
        #tdev=$(cat /proc/partitions  | grep sd[b,c,d,e,f,g,h,i,j,k,l,m,n]1 | awk '{print $4}')
        tdev=`fdisk -l  | grep /dev/sd | grep -v "sda\|Disk" |tail -n 1 | awk '{print $1}'`
        if [ -n $tdev ]; then
            USB_STICK_DEV_FILE=$tdev	
        fi
        if [[ -z ${USB_STICK_DEV_FILE} ]] || [[ ! -b ${USB_STICK_DEV_FILE} ]] ; then
            continue;
        fi
        mntcount=`findmnt $USB_STICK_DEV_FILE | grep $USB_STICK_DEV_FILE | wc -l`
        if [ $mntcount -gt 1 ] ; then
            umount 	$USB_STICK_DEV_FILE > /dev/null 2>&1
            umount 	$USB_STICK_DEV_FILE > /dev/null 2>&1
            umount 	$USB_STICK_DEV_FILE > /dev/null 2>&1
			umount 	$USB_STICK_DEV_FILE > /dev/null 2>&1
        fi	
        findmnt $USB_STICK_DEV_FILE | grep $USB_STICK_DISK > /dev/null
        if [ $? -ne 0 ]; then
            if [ ! -d $USB_STICK_DISK ]; then			
                mkdir -p $USB_STICK_DISK
            fi
            umount $USB_STICK_DISK	> /dev/null 2>&1
            umount $USB_STICK_DEV_FILE > /dev/null 2>&1
            fsck.fat -a $USB_STICK_DEV_FILE > /dev/null 2>&1
            self_mount $USB_STICK_DEV_FILE $USB_STICK_DISK | grep "run fsck" > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                fsck.fat -a $USB_STICK_DEV_FILE > /dev/null 2>&1
                
            fi
            findmnt $USB_STICK_DEV_FILE | grep $USB_STICK_DISK > /dev/null
            if [ $? -ne 0 ]; then
                Error "mount "$USB_STICK_DEV_FILE" "$USB_STICK_DISK" FAILED"	
            #else
             #   Info "mount "$USB_STICK_DEV_FILE" "$USB_STICK_DISK" SUCCESSED"               
            fi            
        fi
        USB_RSYNC_START_USB="TRUE"	
        #if [ -f "${USB_STICK_DISK}/license.txt" ] ; then		
        #    cp $USB_STICK_DISK/license.txt /AIR_FLASH/ > /dev/null 2>&1
        #fi
        rm -f $USB_STICK_DISK/FSCK*  > /dev/null 2>&1
        rm -f ${USB_STICK_DISK}/fsck*  > /dev/null 2>&1
    fi
    #每10s同步一次数据
    if [ $USB_SYNC_TICKS -ge $USB_SYNC_TIME ];then
        USB_SYNC_TICKS=0    
        #同步/6A到外部USB
        if [ $USB_RSYNC_START_USB = "TRUE" ]; then
            licenseFile=$USB_STICK_DISK"/license.txt"
            if [ ! -f $licenseFile ] ; then		   
                Error "---USB disk not legal , need license.txt"
                umount $USB_STICK_DEV_FILE > /dev/null 2>&1
            else                    			
                Info "rsync USB /usbdisk"
                sync_usbdisk		    
            fi
        fi	
    fi

done &
while true
do
    sleep 1 
    SDA_SYNC_TICKS=$[$SDA_SYNC_TICKS+1] 
    #判断sda1盘是否插入
    check_sda1
    if [ $? -ne 1 ] ; then
        continue;
    fi	
    #SATA检查
    check_sda2
    if [ $? -ne 1 ] ; then
        continue;
    fi       
   
    #每10s同步一次数据
    if [ $SDA_SYNC_TICKS -ge $SDA_SYNC_TIME ] ; then
        SDA_SYNC_TICKS=0
        umount $USB_6A_DEV_FILE > /dev/null 2>&1 
        sleep 1
        umount $USB_STICK_FOR_6A > /dev/null 2>&1 
        sleep 1
        self_mount $USB_6A_DEV_FILE /6A | grep "run fsck" > /dev/null 2>&1
        if [ $? -eq 0 ]; then		
            fuser -k /6A > /dev/null 2>&1
            umount $USB_6A_DEV_FILE > /dev/null 2>&1
            sleep 1
            fsck.fat -a $USB_6A_DEV_FILE > /dev/null 2>&1
            self_mount $USB_6A_DEV_FILE /6A > /dev/null 2>&1
            rm $USB_STICK_FOR_6A/FSCK* -f > /dev/null 2>&1
            rm $USB_STICK_FOR_6A/fsck* -f > /dev/null 2>&1
        fi         
        #同步/6A到上传目录/upload/video   
        new_6A_fileLists        
        rsync -av --files-from="${RSYNC_6A_LISTS}" /6A/. ${transfer_path}/. > $RSYNC_tmp 2>&1        
        pNum=$(df | grep $USB_6A_DEV_FILE | awk '{print int($5)}')
        if  [[ $pNum ]] && [[ $pNum -ge 99 ]] ; then
            rsync -av --files-from="${rsync_notList}" /6A/. ${transfer_path}/. >> $RSYNC_tmp 2>&1 
            Info "${USB_6A_DEV_FILE} full rsync notList files"
            echo 1 > "/tmp/.sda1full.tmp"  
            rm -f ${rsync_notList}         
        fi 
        grep "rsync: read errors mapping \"/6A" < $RSYNC_tmp
        if [ $? -eq 0 ]; then                
            Error "rsync: read errors mapping ${fatal_error_6A_times} times---6A kern.log---"   
            cat /var/log/kern.log | grep "FAT-fs" | tail -n 1 >> $SH_LOG
            grep -v "${Netbond}\|${POE1}\|${POE2}" /var/log/kern.log > "${log_path}/kern.log"
            echo "`date +%Y%m%d`-`date +%H%M%S`" > "${log_path}/kerntime"
            Error "-----6A rsync.log--------"  
            cat $RSYNC_tmp >> $SH_LOG  
            fatal_error_6A_times=$[$fatal_error_6A_times+1]  

            tmps=`du -s /6A | awk '{print $1}'`
            if [[ $tmps ]] && [[ "$tmps" -gt 0 ]] 2>/dev/null ; then 
                Error "$USB_6A_DEV_FILE rsync error,but can reading"
            else #cannot RW sda1
                Error "$USB_6A_DEV_FILE error ,need manual intervention"
                echo 1 > "${transfer_path}/.6Aerror" 
                echo 1 > "${transfer_path}/.needmkfs"                           
            fi
    
            fuser -k /6A > /dev/null 2>&1
            umount $USB_6A_DEV_FILE > /dev/null 2>&1 
            sleep 1
            umount $USB_STICK_FOR_6A > /dev/null 2>&1 
            sleep 1
            fsck.fat -a $USB_6A_DEV_FILE > /dev/null 2>&1
            self_mount $USB_6A_DEV_FILE /6A > /dev/null 2>&1
            rm $USB_STICK_FOR_6A/FSCK* -f > /dev/null 2>&1
            rm $USB_STICK_FOR_6A/fsck* -f > /dev/null 2>&1
            findmnt | grep $USB_6A_DEV_FILE | grep ro, > /dev/null 2>&1 
            if [ $? -eq 0 ] ; then
                Error "$USB_6A_DEV_FILE readonly dev"                
            fi
            findmnt | grep $USB_6A_DEV_FILE | grep $USB_STICK_FOR_6A > /dev/null 2>&1 
            if [ $? -eq 0 ] ; then
                tmps=`du -s /6A | awk '{print $1}'`
                if [[ $tmps ]] && [[ "$tmps" -gt 0 ]] 2>/dev/null ; then #can RW sda1
                    rm -fr "${transfer_path}/.6Aerror"                       
                fi
            fi
            if [ $fatal_error_6A_times -ge 3 ] ; then
                Error "$USB_6A_DEV_FILE need mkfs.vfat"
                echo 1 > "${transfer_path}/.needmkfs"
            fi                       
        fi 
        echo "" > $RSYNC_tmp 	
        #find file
#        Info "***rsync SATA ${upload_path}/video/***"
        sync_sda2 "${rsync_lastFiles}"  
        
        check_SATA_sda_RO #check sda1/sda2 readonly ,if sda1 full clean
        check_SATA_sda 
    fi
done
